package com.heima.article.lister;


import com.heima.model.common.constants.article.HotArticleConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.management.Query;

@Component
@Slf4j
public class HotArticleScoreListener {

    @Autowired
    StringRedisTemplate redisTemplate;

    @RabbitListener(queuesToDeclare = @Queue(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE))
    public void hotArticleHandle(String newBehaviorJson){
        log.info(" 接收到文章新行为消息数据   内容==>{}",newBehaviorJson);
        try {
            ListOperations<String, String> opsForList = redisTemplate.opsForList();
            opsForList.rightPush(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_LIST,newBehaviorJson);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(" 向redis list存储数据失败   原因==>{}",e.getMessage());
        }
    }
}
